package net.code.btalk;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Vector;

import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;
import javax.microedition.rms.RecordStoreNotOpenException;

public class SavedData {
//	private static final long USER_INFO_ID = 0x730a7fda314c41dcL;
//	private static final long OPTIONS_ID = 0x6c69ee88659ab5acL;
	
	public static boolean save_account;
	public static boolean use_ssl;
	
	public static boolean ringForCurrent = true;
	public static boolean vibForCurrent = true;
	
	public static boolean ringForOther = true;
	public static boolean vibForOther = true;
	public static boolean ledForOther = true;
	
	
	public static void saveOptions() {
		try {
			RecordStore store = RecordStore.openRecordStore("options", true);
			int numRecord = store.getNumRecords();
			
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			DataOutputStream os = new DataOutputStream(baos);
			os.writeBoolean(ringForCurrent);
			os.writeBoolean(vibForCurrent);
			os.writeBoolean(ringForOther);
			os.writeBoolean(vibForOther);
			os.writeBoolean(ledForOther);
			byte[] data = baos.toByteArray();
			
			if (numRecord == 0) {
				store.addRecord(data, 0, data.length);
			} else {
				store.setRecord(1, data, 0, data.length);
			}
			
			store.closeRecordStore();
		} catch (RecordStoreFullException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RecordStoreNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RecordStoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void readOptions() {
		try {
			RecordStore store = RecordStore.openRecordStore("options", true);
			int numRecord = store.getNumRecords();
			
			if (numRecord > 0) {
				byte[] data = store.getRecord(1);
				
				DataInputStream is = new DataInputStream(new ByteArrayInputStream(data));
				ringForCurrent = is.readBoolean();//os.writeBoolean(ringForCurrent);
				vibForCurrent = is.readBoolean();//os.writeBoolean(vibForCurrent);
				ringForOther = is.readBoolean();//os.writeBoolean(ringForOther);//
				vibForOther = is.readBoolean();//os.writeBoolean(vibForOther);
				ledForOther = is.readBoolean();//os.writeBoolean(ledForOther);
			} 
			
			store.closeRecordStore();
		} catch (RecordStoreFullException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RecordStoreNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RecordStoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void setUserInfo(String username, String password) {
//		PersistentObject store = PersistentStore.getPersistentObject(USER_INFO_ID);
//		Vector up = new Vector();
//		up.addElement(username);
//		up.addElement(password);
//		
//		synchronized (store) {
//			store.setContents(up);
//			store.commit();
//		}
		RecordStore store = null;
		int numRecord = -1;
		try {
			store = RecordStore.openRecordStore("userinfo", true);
			numRecord = store.getNumRecords();
		} catch (RecordStoreFullException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RecordStoreNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RecordStoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// convert user info into byte array
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream os = new DataOutputStream(baos);
		try {
			os.writeBoolean(true);
			os.writeUTF(username);
			os.writeUTF(password);
			os.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// check if the store is empty
		try {
			if (numRecord == 0) {
				byte[] data = baos.toByteArray();
				store.addRecord(data, 0, data.length);
				store.closeRecordStore();
			} else {
				byte[] data = baos.toByteArray();
				store.setRecord(1, data, 0, data.length);
				store.closeRecordStore();
			}
		} catch (RecordStoreNotOpenException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RecordStoreFullException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RecordStoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static Vector getUserInfo() {
//		PersistentObject store = PersistentStore.getPersistentObject(USER_INFO_ID);
//		Vector up;
//		
//		synchronized (store) {
//			up = (Vector) store.getContents();
//		}
//		
//		return up;
		RecordStore store = null;
		int numRecord = -1;
		try {
			store = RecordStore.openRecordStore("userinfo", true);
			numRecord = store.getNumRecords();
		} catch (RecordStoreFullException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RecordStoreNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RecordStoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// empty recordstore
		if (numRecord == 0)
			return null;
		
		try {
			byte[] data = store.getRecord(1);
			DataInputStream is = new DataInputStream(new ByteArrayInputStream(data));
			boolean saved = is.readBoolean();
			
			if (saved) {
				String username = is.readUTF();
				String password = is.readUTF();
				Vector v = new Vector();
				v.addElement(username);
				v.addElement(password);
				store.closeRecordStore();
				return v;
			} else {
				store.closeRecordStore();
				return null;
			}
			
		} catch (RecordStoreNotOpenException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidRecordIDException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RecordStoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public static void destroyUserInfo() {
//		PersistentStore.destroyPersistentObject(USER_INFO_ID);
		RecordStore store = null;
		int numRecord = -1;
		try {
			store = RecordStore.openRecordStore("userinfo", true);
			numRecord = store.getNumRecords();
			
			if (numRecord != 0) {
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				DataOutputStream os = new DataOutputStream(baos);
				
				os.writeBoolean(false);
				os.close();
				byte[] data = baos.toByteArray();
				store.setRecord(1, data, 0, data.length);
				store.closeRecordStore();
			}
		} catch (RecordStoreFullException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RecordStoreNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RecordStoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
